package net.qhzw.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.qhzw.admin.entity.SysDict;
import net.qhzw.admin.mapper.SysDictMapper;
import net.qhzw.admin.param.DictParam;
import net.qhzw.admin.query.DictQuery;
import net.qhzw.admin.service.SysDictService;
import net.qhzw.common.model.BaseQuery;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import net.qhzw.common.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SysDictServiceImpl implements SysDictService {

    @Autowired
    private SysDictMapper dictMapper;


    /***
     * 获取当前字典
     * @param id
     * @return
     */
    @Override
    public JsonResult getDict(String id) {
        SysDict sysDict = dictMapper.selectByPrimaryKey(id);
        return JsonResult.success(sysDict);
    }

    @Override
    public JsonResult getDictList(DictQuery query) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<SysDict> dictList = dictMapper.selectList(query);
        PageInfo<SysDict> pageInfo = new PageInfo<>(dictList);
        result.put("total", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return JsonResult.success(result);
    }

    @Override
    public JsonResult edit(DictParam param) {
        if (StringUtils.isEmpty(param.getId())) {
            SysDict sysDict = new SysDict();
            BeanUtils.copyProperties(param, sysDict);
            sysDict.setCreateTime(new Date());
            sysDict.setId(UUIDUtil.getUUid());
            sysDict.setMark(1);
            sysDict.setStatus(1);
            dictMapper.insertSelective(sysDict);
            return JsonResult.success();
        }
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(param, sysDict);
        dictMapper.updateByPrimaryKeySelective(sysDict);
        return JsonResult.success();
    }

    @Override
    public JsonResult deleteDictList(String id) {
        int i = dictMapper.deleteByPrimaryKey(id);
        return JsonResult.success();
    }


    /***
     * 查询字典表下拉框
     * @param code
     * @return
     */
    @Override
    public JsonResult selectBox(String code) {
        List<Map<String, String>> result = dictMapper.selectCodeBox(code);
        return JsonResult.success(result);
    }
}
